
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XML {
	
	private function[] functions;
	private int use;

	public XML(int N, File file) throws Exception{

		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
		doc.getDocumentElement().normalize();
  		String id = ((Element)doc.getElementsByTagName("functions").item(0)).getAttribute("id");
			
		NodeList funcList = doc.getElementsByTagName("function");
				
		this.functions = new function[funcList.getLength()];

		for (int s = 0; s < funcList.getLength(); s++) {
			String fId = ((Element)funcList.item(s)).getAttribute("id");
			NodeList monList = ((Element)funcList.item(s)).getElementsByTagName("monom");
					
			if(id.equals(fId))
				use = s;
			
			Complex[] coefs = new Complex[monList.getLength()];
			int[] alphas = new int[monList.getLength()];
					
			for(int r = 0; r < monList.getLength(); r++) {
				String real, image, alpha;
    	
				real = ((Element)monList.item(r)).getElementsByTagName("real").item(0).
						getChildNodes().item(0).getNodeValue();
				image = ((Element)monList.item(r)).getElementsByTagName("image").item(0).
						getChildNodes().item(0).getNodeValue();
				alpha = ((Element)monList.item(r)).getElementsByTagName("alpha").item(0).
						getChildNodes().item(0).getNodeValue();
		
				coefs[r] = new Complex(Double.parseDouble(real), Double.parseDouble(image));
				alphas[r] = Integer.parseInt(alpha);
						
				//System.out.println(coefs[r] + "  " + alphas[r]);
			}
					
			this.functions[s] = new function(N, coefs, alphas);
		}
	}
	
	public function getFunction() {return functions[use];}
	
	
	

}
